package com.quectel.core.module.thirdconfig.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.base.tuple.Tuple2;
import com.quectel.base.tuple.Tuples;
import com.quectel.constant.global.SqlConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.thirdconfig.dao.ThirdPlatformConfigDao;
import com.quectel.core.module.thirdconfig.dto.ThirdPlatformConfigDto;
import com.quectel.core.module.thirdconfig.entity.ThirdPlatformConfigEntity;
import com.quectel.core.module.thirdconfig.service.ThirdPlatformConfigService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.LongFunction;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2022/05/09 10:59
 */
@DubboService
public class ThirdPlatformConfigServiceImpl implements ThirdPlatformConfigService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ThirdPlatformConfigServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final LongFunction<String> THIRD_PLATFORM_CONFIG_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "THIRD_PLATFORM_CONFIG_CACHE_BY_ID:%d", id);
    private static final Function<Tuple2<Long, Integer>, String> THIRD_PLATFORM_CONFIG_TENANT_TYPE_FUNC
            = tuple2 -> String.format(RedisCacheConstants.PROJECT_NAME + "THIRD_PLATFORM_CONFIG_TENANT_TYPE:%d:%d"
            , tuple2.getT1(), tuple2.getT2());

    @Autowired
    private ThirdPlatformConfigDao thirdPlatformConfigDao;

    private static final Consumer<ThirdPlatformConfigDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(THIRD_PLATFORM_CONFIG_CACHE_BY_ID_FUNC.apply(dto.getId()));
        CacheKit.invalidRedisCache(THIRD_PLATFORM_CONFIG_TENANT_TYPE_FUNC.apply(Tuples.of(dto.getTenantId(),dto.getType())));

    };

    @Override
    public ThirdPlatformConfigDto selectCacheByTenantAndType(Long tenantId, Integer type) {
        return CacheKit.cacheToRedis(
                () -> selectByTenantAndType(tenantId, type),
                THIRD_PLATFORM_CONFIG_TENANT_TYPE_FUNC.apply(Tuples.of(tenantId, type)),

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public ThirdPlatformConfigDto selectByTenantAndType(Long tenantId, Integer type) {
        ThirdPlatformConfigEntity thirdPlatformConfigEntity = thirdPlatformConfigDao.selectOne(new LambdaQueryWrapper<ThirdPlatformConfigEntity>()
                .eq(ThirdPlatformConfigEntity::getTenantId, tenantId)
                .eq(ThirdPlatformConfigEntity::getType, type)
                .last(SqlConstants.MYSQL_LIMIT_ONE)
        );
        return CopyUtils.copyObj(thirdPlatformConfigEntity, ThirdPlatformConfigDto.class);
    }

    @Override
    public ThirdPlatformConfigDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                THIRD_PLATFORM_CONFIG_CACHE_BY_ID_FUNC.apply(id),

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public ThirdPlatformConfigDto selectById(Long id) {
        ThirdPlatformConfigEntity entity = thirdPlatformConfigDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, ThirdPlatformConfigDto.class));
    }

    @Override
    public List<ThirdPlatformConfigDto> queryList(Map<String, Object> params) {
        List<ThirdPlatformConfigEntity> list = thirdPlatformConfigDao.queryList(params);
        List<ThirdPlatformConfigDto> result = CopyUtils.copyList(list, ThirdPlatformConfigDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private ThirdPlatformConfigDto paddingField(ThirdPlatformConfigDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return thirdPlatformConfigDao.queryTotal(params);
    }

    @Override
    public Long save(ThirdPlatformConfigDto dto) {
        ThirdPlatformConfigEntity entity = CopyUtils.copyObj(dto, ThirdPlatformConfigEntity.class);
        entity.setId(Snowflake.nextId());
        thirdPlatformConfigDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(ThirdPlatformConfigDto dto) {
        ThirdPlatformConfigEntity entity = CopyUtils.copyObj(dto, ThirdPlatformConfigEntity.class);
        thirdPlatformConfigDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(ThirdPlatformConfigDto dto) {
        ThirdPlatformConfigEntity entity = CopyUtils.copyObj(dto, ThirdPlatformConfigEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, thirdPlatformConfigDao);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id) {
        ThirdPlatformConfigDto thirdPlatformConfigDto = selectById(id);

        thirdPlatformConfigDao.deleteById(id);

        CLEAR_CACHE_FUNC.accept(thirdPlatformConfigDto);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
